const express = require("express")
const router = express.Router()
const db = require("../../sqlServer/dbConnect")
const time = require("../../utils/time")
// 权限-- 查询
router.post("/info", (req, res) => {
    const { values, pageConfig,doName } = req.body;
    const { page, pageSize } = pageConfig
    let sql = values === null ? "select * from power limit ?,?;select * from power" : "select * from power where name=?"
    const data = values === null ? [(Number(page) - 1) * Number(pageSize), Number(pageSize)] : [values.name]
    db.sqlQuery(sql, data, (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取信息失败",
                status: 203
            })
        } else {
            // const data = result.filter((item => item.parentId !== 0))
            values === null ?
                res.send({
                    data: result[0],
                    msg: "获取信息成功",
                    total: result[1].length,
                    status: 200
                }) : res.send({
                    data: result,
                    msg: "获取信息成功",
                    total: result.length,
                    status: 200
                })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"权限查询",time.getData()])
})

router.post("/all", (req, res) => {
    db.sqlQuery("select * from power where parentId = 0", (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取信息失败",
                status: 203
            })
        } else {
            res.send({
                data: result,
                msg: "获取信息成功",
                status: 200
            })
        }
    })
})

// 权限-- 添加
router.post("/add", (req, res) => {
    const { name, code, parentId, auth,doName } = req.body
    const createtime = time.getData()
    let sql = "insert into power(name,code,parentId,auth,createtime) values(?,?,?,?,?)"
    const data = [name, code, parentId, auth, createtime]
    db.sqlQuery("select * from power where auth=?", [auth], (err, authList) => {
        if (err) {
            console.log(err)
        } else {
            if (authList && authList.length > 0) {
                res.send({
                    msg: "该权限标识已存在！",
                    status: 203
                })
            } else {
                db.sqlQuery(sql, data, (err) => {
                    if (err) {
                        console.log(err)
                        res.send({
                            msg: "添加失败!",
                            status: 203
                        })
                    } else {
                        res.send({
                            msg: "添加成功!",
                            status: 200
                        })
                    }
                })
            }
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"权限添加",time.getData()])
})

// 权限-- 编辑
router.post("/edit", (req, res) => {
    const { name, code, parentId, auth, Id,doName } = req.body
    let sql = "update power set name=?,code=?,parentId=?,auth=? where Id=?"
    const data = [name, code, parentId, auth, Id]
    db.sqlQuery("select * from power where auth=?", [auth], (err, authList) => {
        if (err) {
            console.log(err)
        } else {
            if(authList.length > 0 && authList[0].Id !== Id){
                res.send({
                    msg:"该权限标识已存在！",
                    status:203
                })
            }else {
                db.sqlQuery(sql, data, (err) => {
                    if (err) {
                        console.log(err)
                        res.send({
                            msg: "编辑失败!",
                            status: 203
                        })
                    } else {
                        res.send({
                            msg: "编辑成功!",
                            status: 200
                        })
                    }
                })
            }
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"权限编辑",time.getData()])
})

// 权限-- 删除
router.post("/delete", (req, res) => {
    const { Id ,doName} = req.body
    let sql = "delete from power where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"权限删除",time.getData()])
})

module.exports = router